package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_CROSS_UNION_EXP_RANK_OPEN_NOTIFY, desc = "商会经验冲榜活动开启通知")
public class CrossUnionExpRankActivityOpenCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossUnionProto.UnionExpRankActivityOpenMsg reqMsg = CrossUnionProto.UnionExpRankActivityOpenMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();

        List<CrossUnionInfo> unionListByServerId = CrossUnionMgr.getUnionListByServerId(serverId);
        for (CrossUnionInfo unionInfo : unionListByServerId) {
            if (unionInfo.isDelete()) {
                continue;
            }
            synchronized (unionInfo){
                if (unionInfo.getRankActivityId() != activityId) {
                    unionInfo.setRankActivityId(activityId);
                    unionInfo.setRankAddExp(0L);
                }
            }
        }

//        for (Map.Entry<String, CrossUnionInfo> unionInfoEntry : CrossUnionMgr.getUnionInfoMap().entrySet()) {
//            CrossUnionInfo unionInfo = unionInfoEntry.getValue();
//            if(unionInfo.getServerId() == serverId){
//                synchronized (unionInfo){
//                    if (unionInfo.getRankActivityId() != activityId) {
//                        unionInfo.setRankActivityId(activityId);
//                        unionInfo.setRankAddExp(0L);
//                    }
//                }
//            }
//        }
    }

    @Override
    public void sendData() {

    }
}
